package com.ccrfid.rmc6.lbservice3.manager;

import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgBloodPressure;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgHeartRate;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgStepTrack;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgTemperature;
import com.ccrfid.rmc6.pojo.Constants;
import com.ccrfid.rmc6.service.personnel.PersonnelService;
import com.ccrfid.rmc6.service.personnel.PersonnelVitalSignAlarmService;
import com.ccrfid.rmc6.service.personnel.PersonnelVitalSignService;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class VitalSignManager {
    private static final Logger logger = LoggerFactory.getLogger(VitalSignManager.class);
    @Autowired
    private EventBus eventBus;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private PersonnelVitalSignService personnelVitalSignService;

    @Autowired
    private PersonnelVitalSignAlarmService personnelVitalSignAlarmService;

    @PostConstruct
    private void init() {
        eventBus.register(this);
    }

    //心率
    @Subscribe
    private void onMsgHeartRate(MsgHeartRate msgHeartRate) {
        PersonnelEntity personnelEntity = personnelService.getPersonnelByCardNumber(msgHeartRate.getCardNumber());
        if (personnelEntity != null) {
            if (personnelEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
                //记录历史轨迹
                personnelVitalSignService.recordHeartRate(personnelEntity.getId(), msgHeartRate);
                //人员心率预警
                logger.info("获取到心率数据开始判定是否异常"+ msgHeartRate);
                personnelVitalSignAlarmService.heartRateAlarm(msgHeartRate, personnelEntity);
                logger.info("心率判定结束");
            } else {
                logger.trace("ignore personnel heartRate - personnel is disabled, {}-{}",
                        personnelEntity.getCardNumber(), personnelEntity.getPersonnelName());
            }
        }
    }

    //血压
    @Subscribe
    private void onMsgBloodPressure(MsgBloodPressure msgBloodPressure) {
        PersonnelEntity personnelEntity = personnelService.getPersonnelByCardNumber(msgBloodPressure.getCardNumber());
        if (personnelEntity != null) {
            if (personnelEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
                //记录历史轨迹
                personnelVitalSignService.recordBloodPressure(personnelEntity.getId(), msgBloodPressure);
                //人员血压预警
                logger.info("获取到血压数据开始判定是否异常"+ msgBloodPressure);
                personnelVitalSignAlarmService.bloodPressureAlarm(msgBloodPressure, personnelEntity);
                logger.info("血压判定结束");
            } else {
                logger.trace("ignore personnel bloodPressure - personnel is disabled, {}-{}",
                        personnelEntity.getCardNumber(), personnelEntity.getPersonnelName());
            }
        }
    }

    //计步
    @Subscribe
    private void onMsgStepTrack(MsgStepTrack msgStepTrack) {
        PersonnelEntity personnelEntity = personnelService.getPersonnelByCardNumber(msgStepTrack.getCardNumber());
        if (personnelEntity != null) {
            if (personnelEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
                //记录历史轨迹
                personnelVitalSignService.recordStepTrack(personnelEntity.getId(), msgStepTrack);
            } else {
                logger.trace("ignore personnel stepTrack - personnel is disabled, {}-{}",
                        personnelEntity.getCardNumber(), personnelEntity.getPersonnelName());
            }
        }
    }

    //体温
    @Subscribe
    private void onMsgTemperature(MsgTemperature msgTemperature) {
        PersonnelEntity personnelEntity = personnelService.getPersonnelByCardNumber(msgTemperature.getCardNumber());
        if (personnelEntity != null) {
            if (personnelEntity.getEnabled().equals(Constants.ENABLE_STATUS.ENABLE.id)) {
                //记录历史轨迹
                personnelVitalSignService.recordTemperature(personnelEntity.getId(), msgTemperature);
                //人员体温预警
               personnelVitalSignAlarmService.temperatureAlarm(msgTemperature, personnelEntity);
            } else {
                logger.trace("ignore personnel temperature - personnel is disabled, {}-{}",
                        personnelEntity.getCardNumber(), personnelEntity.getPersonnelName());
            }
        }
    }
}
